package com.ltv.xiangqiplugin.controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import com.ltv.xiangqiplugin.models.Messages;
import com.ltv.xiangqiplugin.utils.MoveUtils;
import com.ltv.xiangqiplugin.utils.XiangPluginConfiguration;
import com.ltv.xiangqiplugin.views.ChessApp;

public class GameActionControl implements ActionListener {
    private ChessApp mainApp;
    
    public GameActionControl(ChessApp mainApp) {
        this.mainApp = mainApp;
        
        mainApp.getPositionStack().addPostion(mainApp.getBoardView().getGame().getPosition());
        
        mainApp.getMoveStack().addMove("start");
        mainApp.getMoveStack().addVnMove("start");
    }
    
    @Override
    public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand();
        
        // Fix bug
        mainApp.getBoardViewControl().setOriginPanel(null);
        
        if (command.compareTo("NEXT") == 0) {
            mainApp.getPositionStack().next();
            mainApp.getMoveStack().next();
        } else if (command.compareTo("PREV") == 0) {
            mainApp.getPositionStack().prev();
            mainApp.getMoveStack().prev();
        } else if (command.compareTo("LAST") == 0) {
            mainApp.getPositionStack().last();
            mainApp.getMoveStack().last();
        } else if (command.compareTo("FIRST") == 0) {
            mainApp.getPositionStack().first();
            mainApp.getMoveStack().first();
        } else if (command.compareTo("ROLLBACK") == 0) {
            mainApp.getPositionStack().setPositions(ChessApp.getBufferedPositions());
            mainApp.getMoveStack().setMoves(ChessApp.getBufferedMoves());
            mainApp.getMoveStack().setVnMoves(ChessApp.getBufferedVnMoves());
            
            mainApp.getPositionStack().setIndex(ChessApp.getBufferedIndex());
            mainApp.getMoveStack().setIndex(ChessApp.getBufferedIndex());
            
            mainApp.getPositionStack().notifyObservers();
            mainApp.getMoveStack().notifyObservers(true);
            
            mainApp.getGameActionView().disableRollbackButton();
        }  else if (command.compareTo("REVERSE") == 0) {
            mainApp.getBoardView().setReverse(!mainApp.getBoardView().isReverse());
        } else if (command.compareTo("HTML") == 0) {
            Vector<String> moves = mainApp.getMoveStack().getMoves();
            
            int size = moves.size();
            String moveList = "";
            String move = "";
            for (int i = size - 2; i >= 0; i--) {
                move = moves.get(i);
                move = MoveUtils.getStandardPGNMove(move);
                moveList += move + "+";
            }
            
            if (moveList.length() > 0) {
                moveList = moveList.substring(0, moveList.length() - 1);
            }
            
            moveList = moveList.toString().toUpperCase();
            Vector<String> positions = mainApp.getPositionStack().getPositions();
            
            String version = XiangPluginConfiguration.getInstance().getPluginVersion();
            
            String tab = "    ";
            
            // Using <APPLET> tag
//            String html = "<applet archive=\"http://" +
//                    XiangPluginConfiguration.getInstance().getGaeApplication() +
//                    ".appspot.com/views/plugin/" +
//                    version +
//                    "/xiangqiplugin.jar\" code=\"com.ltv.xiangqiplugin.views.ChessApp\" width=400 height=350> " +
//                    "name=\"" +
//                    System.nanoTime() +
//                    "\"";
//            html += "\n";
//            html += tab + "<param name=initPosition value=\"" + positions.get(positions.size() - 1) + "\"/>";
//            html += "\n";
//            html += tab + "<param name=moveList value=\"" + moveList + "\"/>";
//            html += "\n";
//            html += tab + "<param name=startIndex value=\"" + (positions.size() - 1 - mainApp.getPositionStack().getIndex()) + "\"/>";
//            html += "\n";
//            html += tab + "<param name=boardSize value=\"" + "small" + "\"/>";
//            html += "\n";
//            html += tab + "<param name=language value=\"" + Messages.getInstance().getLang() + "\"/>";
//            html += "\n";
//            html += tab + "<param name=isActive value=\"" + "false" + "\"/>";
//            html += "\n";
//            html += "</applet>";
            
            // Using <OBJECT> and <EMBED> tags
//            String html = "";
//            long current = System.nanoTime();
//            html += "<object id=\"" + current + "-object\"";
//            html += "\n";
//            
//            html += tab + tab + "classid=\"clsid:8AD9C840-044E-11D1-B3E9-00805F499D93\"";
//            html += "\n";
//            
//            html+= tab + tab + "width=\"400\" height=\"350\"";
//            html += "\n";
//            
//            html += tab + tab + "codebase=\"http://java.sun.com/update/1.6.0/jinstall-6u30-windows-i586.cab#Version=1,6,0,0\">";
//            html += "\n";
//            
//            html += tab + "<param name=\"archive\" value=\"http://" +
//                    XiangPluginConfiguration.getInstance().getGaeApplication() +
//                    ".appspot.com/views/plugin/" +
//                    version +
//                    "/xiangqiplugin.jar" +
//                    "\" />";
//            html += "\n";
//            
//            html += tab + "<param name=\"code\" value=\"com.ltv.xiangqiplugin.views.ChessApp\" />";
//            html += "\n";
//            
//            html += tab + "<param name=\"initPosition\" value=\"" + positions.get(positions.size() - 1) + "\"/>";
//            html += "\n";
//            
//            html += tab + "<param name=\"moveList\" value=\"" + moveList + "\"/>";
//            html += "\n";
//            
//            html += tab + "<param name=\"startIndex\" value=\"" + (positions.size() - 1 - mainApp.getPositionStack().getIndex()) + "\"/>";
//            html += "\n";
//            
//            html += tab + "<param name=\"boardSize\" value=\"" + "small" + "\"/>";
//            html += "\n";
//            
//            html += tab + "<param name=\"language\" value=\"" + Messages.getInstance().getLang() + "\"/>";
//            html += "\n";
//            
//            html += tab + "<param name=\"isActive\" value=\"" + "true" + "\"/>";
//            html += "\n";
//            
//            html += tab + "<param name=\"MAYSCRIPT\" value=\"" + "true" + "\">";
//            html += "\n";
//            
//            html += tab + "<embed id=\"" + current + "-embed\"";
//            html += "\n";
//            
//            html += tab + tab + "type=\"application/x-java-applet;version=1.6\"";
//            html += "\n";
//            
//            html += tab + tab + "width=\"400\" height=\"350\"";
//            html += "\n";
//            
//            html += tab + tab + "archive=\"" +
//                    XiangPluginConfiguration.getInstance().getGaeApplication() +
//                    ".appspot.com/views/plugin/" +
//                    version +
//                    "/xiangqiplugin.jar" +
//                    "\"";
//            html += "\n";
//            
//            html += tab + tab + "code=\"com.ltv.xiangqiplugin.views.ChessApp\"";
//            html += "\n";
//            
//            html += tab + tab + "pluginspage=\"http://java.com/download/\"";
//            html += "\n";
//            
//            html += tab + tab + "initPosition=\"" + positions.get(positions.size() - 1) + "\"";
//            html += "\n";
//            
//            html += tab + tab + "moveList=\"" + moveList + "\"";
//            html += "\n";
//            
//            html += tab + tab + "startIndex=\"" + (positions.size() - 1 - mainApp.getPositionStack().getIndex()) + "\"";
//            html += "\n";
//            
//            html += tab + tab + "boardSize=\"small\"";
//            html += "\n";
//            
//            html += tab + tab + "language=\"" + Messages.getInstance().getLang() + "\"";
//            html += "\n";
//            
//            html += tab + tab + "isActive=\"" + "true" + "\"";
//            html += "\n";
//            
//            html += tab + tab + "MAYSCRIPT=\"" + "true" + "\">";
//            html += "\n";
//            
//            html += tab + "</embed>";
//            html += "\n";
//            
//            html += "</object>";
            
            String html = "";
            long current = System.nanoTime();
            
            html += "<script>\n";
            html += "if (navigator.appName == 'Microsoft Internet Explorer') {\n";
            html += "    document.write(\n";
            html += "        '<object id=\"" + current + "\"' +\n";
            html += "        '    classid=\"" + XiangPluginConfiguration.getInstance().getObjectClassID() + "\"' +\n";
            html += "        '        width=\"400\" height=\"350\"' +\n";
            html += "        '        codebase=\"" + XiangPluginConfiguration.getInstance().getObjectCodeBase() + "\">' +\n";
            html += "        '    <param name=\"archive\" value=\"http://" + "sachcotuongvn-dev" + ".appspot.com/views/plugin/" + "1.0.0" + "/xiangqiplugin.jar\" />' +\n";
            html += "        '    <param name=\"code\" value=\"com.ltv.xiangqiplugin.views.ChessApp\" />' +\n";
            html += "        '    <param name=\"initPosition\" value=\""+ positions.get(positions.size() - 1) + "\"/>' +\n";
            html += "        '    <param name=\"moveList\" value=\"" + moveList + "\"/>' +\n";
            html += "        '    <param name=\"startIndex\" value=\"" + (positions.size() - 1 - mainApp.getPositionStack().getIndex()) + "\"/>' +\n";
            html += "        '    <param name=\"boardSize\" value=\"" + "small" + "\"/>' +\n";
            html += "        '    <param name=\"language\" value=\"" + Messages.getInstance().getLang() + "\"/>' +\n";
            html += "        '    <param name=\"theme\" value=\"" + Messages.getInstance().getTheme().getValue() + "\"/>' +\n";
            html += "        '    <param name=\"isActive\" value=\"true\"/>' +\n";
            html += "        '    <param name=\"MAYSCRIPT\" value=\"true\">' +\n";
            html += "        '</object>');\n";
            html += "} else {\n";
            html += "    document.write(\n";
            html += "        '<embed id=\"" + current + "\"' +\n";
            html += "        '    type=\"application/x-java-applet\"' +\n";
            html += "        '    width=\"400\" height=\"350\"' +\n";
            html += "        '    archive=\"http://" + "sachcotuongvn-dev" + ".appspot.com/views/plugin/" + "1.0.0" + "/xiangqiplugin.jar\"' +\n";
            html += "        '    code=\"com.ltv.xiangqiplugin.views.ChessApp\"' +\n";
            html += "        '    pluginspage=\"" + XiangPluginConfiguration.getInstance().getEmbedPluginsPage() + "\"' +\n";
            html += "        '    initPosition=\"" + positions.get(positions.size() - 1) + "\"' +\n";
            html += "        '    moveList=\"" + moveList + "\"' +\n";
            html += "        '    startIndex=\"" + (positions.size() - 1 - mainApp.getPositionStack().getIndex()) + "\"' +\n";
            html += "        '    boardSize=\"" + "small" + "\"' +\n";
            html += "        '    language=\"" + Messages.getInstance().getLang() + "\"' +\n";
            html += "        '    theme=\"" + Messages.getInstance().getTheme().getValue() + "\"' +\n";
            html += "        '    isActive=\"true\"' +\n";
            html += "        '    MAYSCRIPT=\"true\">' +\n";
            html += "        '</embed>');\n";
            html += "}\n";
            html += "</script>";
            
//            mainApp.getWin().call("exportHTML", new String[] {html});
            mainApp.getHtmlView().setHtmlText(html);
        } else if (command.compareTo("OPTION") == 0) {
            mainApp.getOptionView().setVisible(true);
        }
        
    }
    
}
